package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.service.ImageScanService;
import com.aliyuncs.service.TextScanService;
import com.heima.article.ArticleApi;
import com.heima.minio.service.FileUploadServer;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.wemedia.pojo.WmNews;
import com.heima.model.common.wemedia.vo.ContentVo;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmNewsAutoScanService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WmNewsAutoScanServiceImpl implements WmNewsAutoScanService {
    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private FileUploadServer fileUploadServer;
    @Autowired
    private TextScanService textScan;
    @Autowired
    private ImageScanService imageScan;
    @Autowired
    private ArticleApi articleApi;
    @Autowired
    private WmUserMapper wmUserMapper;
    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Async
    /**
     * 功能描述: 自动审核并发布文章
     *
     * @param wmNews
     * @return:
     */
    @Override
    public void autoScan(WmNews wmNews) {
        //1.解析出文本和图片内容
        String content = wmNews.getContent();
        List<ContentVo> contentVo = JSON.parseArray(content, ContentVo.class);
        //1.1获取文本内容
        String text = contentVo.stream()
                .filter(contentvo1 -> "text".equals(contentvo1.getType()))
                .map(ContentVo::getValue)
                .collect(Collectors.joining());
        //1.2获取图片的地址集合
        List<String> images = contentVo.stream()
                .filter(contentvo1 -> "image".equals(contentvo1.getType()))
                .map(ContentVo::getValue)
                .collect(Collectors.toList());
        //2.文章文本审核
        try {
            Map<String,String> map = textScan.textScan(text);
            if (map!=null){
                //未通过
                String label = map.get("label");
                wmNews.setStatus((short)2);
                log.warn("命中的垃圾分类{}",label);
                wmNewsService.updateById(wmNews);
                return;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //3.文章图片审核
        //3.1从minio中获取字节数组
        ArrayList<byte[]> byteList = new ArrayList<>();
        for (String image : images) {
            byte[] bytes = fileUploadServer.downloadFileWithByte(image);
            byteList.add(bytes);
        }
        //3.2处理文件审核
        try {
            Map<String,String> map = imageScan.imageScan(byteList);
            if (!"pass".equals(map.get("suggestion"))) {
                //图片审核未通过
                log.warn("命中的垃圾分类{}",map.get("label"));
                wmNews.setStatus((short)2);
                wmNewsService.updateById(wmNews);
                return;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //4.审核通过保存app端相关文章
        //作者名
        Integer userId = wmNews.getUserId();
        String authorName = wmUserMapper.selectById(userId).getName();
        //频道名
        String channelName = wmChannelMapper.selectById(wmNews.getChannelId()).getName();
        //远程调用
        ResponseResult result = articleApi.add(wmNews, authorName, channelName);
        Long articleId = (Long) result.getData();
        wmNews.setArticleId(articleId);
        wmNewsService.updateById(wmNews);
    }

}
